package com.example.cnotebook;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.cnotebook.adapter.CategoryGridViewAdapter;
import com.example.cnotebook.pojo.CategoryInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class CategoryActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener, OKHttpUtil.CallBack, AdapterView.OnItemClickListener {
    private ImageView ivBack;
    private GridView gridView;

    private List<CategoryInfo> categorys;
    private CategoryGridViewAdapter gridViewAdapter;
    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        ivBack = findViewById(R.id.iv_back);
        gridView = findViewById(R.id.gridView);

        ivBack.setOnClickListener(this);
        gridView.setOnItemClickListener(this);
        gridView.setOnItemLongClickListener(this);

        categorys = new ArrayList<>();
        gridViewAdapter = new CategoryGridViewAdapter(this, categorys);
        gridView.setAdapter(gridViewAdapter);
        getData();
    }

    private void getData() {
        categorys.clear();
        OKHttpUtil.get("/category/list/" + UserConstant.userInfo.getId(), this, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(JSONObject response) throws JSONException {
        if (response.getInt("code") == 1000) {
            JSONArray data = response.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                CategoryInfo categoryInfo = gson.fromJson(jsonObject.toString(), CategoryInfo.class);
                categorys.add(categoryInfo);
            }
            System.out.println(categorys);
            gridViewAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(int errorNo, String strMsg) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CategoryInfo categoryInfo = categorys.get(position);
        Intent intent = new Intent(this, NotesActivity.class);
        intent.putExtra("categoryId", categoryInfo.getId());
        intent.putExtra("categoryName", categoryInfo.getName());
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        System.out.println(position);
        new AlertDialog.Builder(this)
                .setTitle("删除分类")
                .setMessage("你确定要永久删除该分类嘛？该操作会永久删除分类下所以笔记！无法恢复！")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OKHttpUtil.post("/category/delete/" + categorys.get(position).getId(), new JSONObject(), new OKHttpUtil.CallBack() {
                            @Override
                            public void onSuccess(JSONObject response) throws JSONException {
                                gridViewAdapter.notifyDataSetChanged();
                                UserConstant.homeIsSaveData = true;
                                UserConstant.userIsSaveData = true;
                                CategoryInfo ci = categorys.get(position);
                                for (int i = 0; i < UserConstant.categoryInfos.size(); i++) {
                                    CategoryInfo categoryInfo = UserConstant.categoryInfos.get(i);
                                    if (categoryInfo.getId().equals(ci.getId())){
                                        UserConstant.categoryInfos.remove(i);
                                        break;
                                    }
                                }
                                categorys.remove(position);
                                Toast.makeText(CategoryActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFailure(int errorNo, String strMsg) {

                            }
                        }, CategoryActivity.this);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();
        return true;
    }
}
